package MainTest;

import java.io.File;

/**
 * Created by xuyi on 2017/2/12.
 */
public class MvnFile {

    public void delete(File file) {

        if (file.isDirectory()) {
            String[] listFile = file.list();
            if (listFile.length == 0) { // 为空，删除空文件夹
                String fileName = file.getName();
                String fileAbsolutePath = file.getAbsolutePath();
                file.delete();
                System.out.println("delete empty dir -> " + file.getAbsolutePath());
                // 继续删除 可能因为删除一次之后 使文件夹变成空文件夹 的情况
                String subPath = fileAbsolutePath.substring(0, fileAbsolutePath.length() - fileName.length() - 1);
                this.delete(new File(subPath));
                return;
            }
            for (int i = 0; i < listFile.length; i++) {
                String absolutePath = file.getAbsolutePath();
                String filePath = absolutePath + "\\" + listFile[i];
                this.delete(new File(filePath));
            }
        } else if (file.isFile()){ // 是文件
            String fileName = file.getName();
            String fileAbsolutePath = file.getAbsolutePath();
            if (!fileName.endsWith(".jar")) {
                file.delete();
                System.out.println("delete file -> " + fileAbsolutePath);
                // 继续删除 可能因为删除一次之后 使文件夹变成空文件夹 的情况
                String subPath = fileAbsolutePath.substring(0, fileAbsolutePath.length() - fileName.length() - 1);
                this.delete(new File(subPath));
            }
        }
    }

    public static void main(String[] args) {

        String path = "C:\\Users\\xuyix\\.m2\\repository";

        File file = new File(path);

        MvnFile mvnFile = new MvnFile();

        mvnFile.delete(file);
    }

}
